import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import Welcome from "../pages/Welcome.vue";

import FailureReport from "../pages/Failure/report/index";
import FailureReportAdd from "../pages/Failure/reportAdd/index";
import FailureReportEdit from "../pages/Failure/reportEdit/index";

import bifurcateQuery from "../pages/bifurcate/query/index";
import bifurcateAdd from "../pages/bifurcate/add/index";
import bifurcateRecord from "../pages/bifurcate/record/index";
import bifurcateState from "../pages/bifurcate/state/index";

import telesemeQuery from "../pages/teleseme/query/index";
import telesemeAdd from "../pages/teleseme/add/index";
import telesemeRecord from "../pages/teleseme/record/index";
import telesemeState from "../pages/teleseme/state/index";

import axleSgaftQuery from "../pages/axleSgaft/query/index";
import axleSgaftAdd from "../pages/axleSgaft/add/index";
import axleSgaftRecord from "../pages/axleSgaft/record/index";
import axleSgaftState from "../pages/axleSgaft/state/index";

import homePage from "../pages/homePage/index";
import stateWatch from "../pages/stateWatch/index";
import workBoard from "../pages/workBoard/index";

Vue.use(VueRouter);

const router = new VueRouter({
    routes: [
        {
            path: "/login",
            component: Login,
        },
        {
            path: "/",
            redirect: "/login",
        },
        {
            path: "/home",
            component: Home,
            redirect: "/homePage",
            children: [
                {
                    name: "homePage",
                    path: "/homePage",
                    component: homePage,
                },
                {
                    name: "stateWatch",
                    path: "/stateWatch",
                    component: stateWatch,
                },
                {
                    path: "/welcome",
                    component: Welcome,
                },
                {
                    path: "/failureReport",
                    component: FailureReport,
                },
                {
                    path: "/failureReportAdd",
                    component: FailureReportAdd,
                },
                {
                    path: "/failureReportEdit",
                    component: FailureReportEdit,
                },
                {
                    path: "/bifurcateQuery",
                    component: bifurcateQuery,
                },
                {
                    path: "/bifurcateAdd",
                    name: "bifurcateAdd",
                    component: bifurcateAdd,
                },
                {
                    path: "/bifurcateRecord",
                    name: "bifurcateRecord",
                    component: bifurcateRecord,
                },
                {
                    path: "/bifurcateState",
                    name: "bifurcateState",
                    component: bifurcateState,
                },
                {
                    path: "/telesemeQuery",
                    component: telesemeQuery,
                },
                {
                    path: "/telesemeAdd",
                    name: "telesemeAdd",
                    component: telesemeAdd,
                },
                {
                    path: "/telesemeRecord",
                    name: "telesemeRecord",
                    component: telesemeRecord,
                },
                {
                    path: "/telesemeState",
                    name: "telesemeState",
                    component: telesemeState,
                },
                {
                    path: "/axleSgaftQuery",
                    component: axleSgaftQuery,
                },
                {
                    path: "/axleSgaftAdd",
                    name: "axleSgaftAdd",
                    component: axleSgaftAdd,
                },
                {
                    path: "/axleSgaftRecord",
                    name: "axleSgaftRecord",
                    component: axleSgaftRecord,
                },
                {
                    path: "/axleSgaftState",
                    name: "axleSgaftState",
                    component: axleSgaftState,
                },
                {
                    path: "/workBoard",
                    name: "workBoard",
                    component: workBoard,
                },
            ],
        },
    ],
});

// 拦载路由导航守卫
router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //from 从哪个路径跳转
    //next()放行  next('/login') 强制跳转

    if (to.path === "/login") next();
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) return next("/login");
    console.log(to.path);
    if (
        to.path == "/bifurcateRecord" ||
        to.path == "/telesemeRecord" ||
        to.path == "/axleSgaftRecord"
    ) {
        Vue.prototype
            .$prompt("请输入密码", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                inputType: "password",
            })
            .then(({ value }) => {
                console.log(value);
                if (value == "111") {
                    next();
                } else {
                    return Vue.prototype.$message({
                        type: "error",
                        message: "密码错误",
                    });
                }
            })
            .catch(() => {
                Vue.prototype.$message({
                    type: "info",
                    message: "取消输入",
                });
            });
    } else {
        next();
    }
});

export default router;
